package com.akk.module.avs.service;

import com.akk.module.avs.entity.AvsApplyLoan;
import com.akk.module.avs.entity.AvsApplyLoanExample;
import com.akk.module.avs.entity.AvsLoanCon;
import com.akk.module.avs.entity.AvsLoanConExample;
import com.akk.module.avs.mapper.AvsApplyLoanMapper;
import com.akk.module.avs.mapper.AvsLoanConMapper;
import com.akk.module.avs.mapper.dao.AvsApplyLoanDao;
import com.akk.module.avs.mapper.dao.AvsLoanConDao;
import com.akk.module.avs.model.request.AvsApplyLoanRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AvsApplyLoanService {

    @Autowired
    private AvsApplyLoanMapper applyLoanMapper;

    @Autowired
    private AvsApplyLoanExample applyLoanExample;

    @Autowired
    private AvsApplyLoanDao applyLoanDao;

    @Autowired
    private AvsLoanConMapper loanConMapper;

    @Autowired
    private AvsLoanConExample loanConExample;

    @Autowired
    private AvsLoanConDao loanConDao;


    public List<AvsApplyLoan> queryByParams(Map params){
        applyLoanExample.clear();
        AvsApplyLoanExample.Criteria criteria = applyLoanExample.createCriteria();
        if (params.containsKey("status"))
            criteria.andLoanStatusEqualTo(Integer.valueOf(params.get("status").toString()));
        if (params.containsKey("notstatus"))
            criteria.andLoanStatusNotEqualTo(Integer.valueOf(params.get("notstatus").toString()));
        applyLoanExample.setOrderByClause("loan_index desc");
        return applyLoanMapper.selectByExample(applyLoanExample);
    }

    public void updateStatusNomralForAll(){
        applyLoanDao.updateStatusNomralForAll();
    }

    public AvsApplyLoan queryById(Long loanId){
        AvsApplyLoan loan = applyLoanMapper.selectByPrimaryKey(loanId);
        loanConExample.clear();
        loanConExample.createCriteria().andLoanIdEqualTo(loanId);
        loan.setCons(loanConMapper.selectByExample(loanConExample));
        return loan;
    }

    @Transactional
    public void save(AvsApplyLoan loan){
        Gson gson = new Gson();
        String json = loan.getConsStr();
        json = getFromBase64(json);
        applyLoanDao.insert(loan);
        List<AvsLoanCon> cons = gson.fromJson(json,new TypeToken<List<AvsLoanCon>>(){}.getType());
        if (cons.size() == 0)
            return;
        loanConExample.clear();
        loanConExample.createCriteria().andLoanIdEqualTo(loan.getLoanId());
        loanConMapper.deleteByExample(loanConExample);
        Map params = new HashMap();
        params.put("loanId",loan.getLoanId());
        params.put("cons",cons);
        loanConDao.insertBatch(params);
    }

    public void delete(Long loanId){
        applyLoanMapper.deleteByPrimaryKey(loanId);
    }

    public static String getFromBase64(String s) {
        byte[] b;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Transactional
    public void update(AvsApplyLoan entity) {
        applyLoanMapper.updateByPrimaryKey(entity);
        Gson gson = new Gson();
        String json = entity.getConsStr();
        json = getFromBase64(json);
        List<AvsLoanCon> cons = gson.fromJson(json,new TypeToken<List<AvsLoanCon>>(){}.getType());
        if (cons.size() == 0)
            return;
        loanConExample.clear();
        loanConExample.createCriteria().andLoanIdEqualTo(entity.getLoanId());
        loanConMapper.deleteByExample(loanConExample);
        Map params = new HashMap();
        params.put("loanId",entity.getLoanId());
        params.put("cons",cons);
        loanConDao.insertBatch(params);
    }
}
